# 游戏开始后，新建线程进行传感器采集的逻辑在这里
import smbus
#import serial
import time
import numpy as np
import matplotlib.pyplot as plt
import json
from threading import Thread

class reference_int:
    def __init__(self):
        self.randomNum=1
# ser = serial.Serial("/dev/cu.usbserial-1130",9600)#winsows系统使用com1口连接串行口
#ser = serial.Serial("/dev/ttyS0",115200)
class MysqlInsert(Thread):
    def __init__(self,ri:reference_int):
        super().__init__()
        self.i=ri
        self.flowData=[]
        self.pressData=[]
        self.count=0
        self.bus = smbus.SMBus(1)
        self.address1 = 0x50
        self.address2 = 0x25
        self.flag=True
        self.startTime=0
        self.endTime=0
        self.flag=True
        self.First=0
    def run(self):
        print("Rundaole")
        self.MYsql()
    
    def MYsql(self):
        while self.flag:
            flow = self.bus.read_byte_data(self.address1,5)
#             print("@"+str(flow))
            self.i.randomNum=flow-98
#             press = self.bus.read_byte_data(self.address2,1)
#             print(str(flow)+'1')
            if flow!=98:
#                 if flag:
#                     self.startTime=time.time
#                     flag=False
#                 print("@"+str(flow))
                self.flowData.append(flow)
#                 self.pressData.append(press)
                self.count=1
#                 self.endTime=time.time
#                 if (endTime-startTIme)>1:
#                     for item in flowData:
#                         FirstSum+=item
#                     self.First=FirstSum
            if flow==98 and self.count==1:
#                 print(str(flow)+'2')
#                 print('flow is'+str(self.flowData))
#                 print('press is'+str(self.pressData))
                # flowStrData=str(self.flowData)
                # pressStrData=str(self.pressData)
                # DataRes=["TestUser",flowStrData,pressStrData]
                self.count=self.count-1
                #print(flow)#+ "SLPM"
                #print(press)
            time.sleep(0.02)
    
        